package com.Tao.json;

import com.Tao.dao.UserDao;
import com.Tao.user.FirstPageJson;
import com.Tao.user.User;
import com.Tao.util.JDBCUtil;
import com.Tao.utils.MybatisUtils;
import com.alibaba.fastjson.JSON;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/a")
public class JsonTest extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        Integer page = Integer.valueOf(req.getParameter("page"));
        Integer size = Integer.valueOf(req.getParameter("limit"));
        Map<String, Object> map = new HashMap<>();
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        List<FirstPageJson> userListLimit = mapper.getUserListLimit((page-1)*10, size);
        Integer count = mapper.count();
        //List<User> list = JDBCUtil.getList(User.class, "select * from test_table limit ?,?", (page - 1) * size, size);
        //Integer count = JDBCUtil.getCount("select count(1) from test_table");
        map.put("code",0);
        map.put("msg","查询成功");
        map.put("count",count);
        map.put("data",userListLimit);
        resp.getWriter().write(JSON.toJSONString(map));
        sqlSession.close();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
